import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'font-awesome/css/font-awesome.min.css'
import App from './App.vue'
import router from './router'
import store from './store'

import {initMenu} from "./utils/menus";
import {postKeyValueRequest} from "./utils/api";
import {postRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";

Vue.prototype.postKeyValueRequest = postKeyValueRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;

Vue.config.productionTip = false

Vue.use(ElementUI,{size:'medium'});

//全局前置守卫
router.beforeEach((to, from, next) => {
    //to: Route: 即将要进入的目标 路由对象
    //from: Route: 当前导航正要离开的路由
    if(to.path =='/'){
      next();
    }else{
        if(window.sessionStorage.getItem("user")){
            initMenu(router,store);
            next();
        }else{
            next('/?redirect=' + to.path);
        }
    }
})

// 注册一个全局自定义指令 `v-focus`
Vue.directive('focus', {
    // 当被绑定的元素插入到 DOM 中时……
    inserted: function (el) {
        // 聚焦元素
        el.querySelector('input').focus();
    }
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
